package genedit.gui.propertyDialogs;



import genedit.gui.GMainFrame;
import genedit.gui.exceptions.RequiredFieldException;
import genedit.gui.propertyDialogs.AbstractDialog.StanjeDijaloga;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class AddUpdateDialog  extends JDialog {
	
	public static final int OK = 0;
	public static final int CANCEL = 1;
	 JButton btnCancel;
	 JButton btnOk;
	
	AbstractPanelDetail panelDetail;
	private int closingMode = AddUpdateDialog.CANCEL;
	
	public AddUpdateDialog()
	{
		
	}
	
	public AddUpdateDialog(JDialog parent, String title, StanjeDijaloga mode, AbstractPanelDetail panDetail) {

		super(parent, title, true);
		
		this.panelDetail = panDetail;
		add(panDetail, BorderLayout.CENTER);
		
		JPanel panSouth = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		 btnOk = new JButton(GMainFrame.getInstance().getResourceBundle().getString("btnOk"));
		btnOk.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				closingMode = AddUpdateDialog.OK;
		
					try {
						panelDetail.proveriIspravnostPodataka();
						setVisible(false);

					} catch (RequiredFieldException e) {
						JOptionPane.showMessageDialog(AddUpdateDialog.this, GMainFrame.getInstance().getResourceBundle().getString("Poruka"),
								GMainFrame.getInstance().getResourceBundle().getString("Greska"), JOptionPane.ERROR_MESSAGE);
						closingMode = AddUpdateDialog.CANCEL;
					}
				}
					
			
		});
		
		btnCancel = new JButton(GMainFrame.getInstance().getResourceBundle().getString("btnCancel"));
		btnCancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				closingMode = AddUpdateDialog.CANCEL;
				setVisible(false);
			}
		});
		
		panSouth.add(btnOk);
		panSouth.add(btnCancel);
		add(panSouth, BorderLayout.SOUTH);
		
		pack();
		setLocationRelativeTo(parent);
		btnOk.setText(GMainFrame.getInstance().getResourceBundle().getString("btnOk"));
		btnCancel.setText(GMainFrame.getInstance().getResourceBundle().getString("btnCancel"));

	}
	

	public int getClosingMode() {
		return closingMode;
	}
	
	public AbstractPanelDetail getPanelDetail() {
		return panelDetail;
		}

}
